package com.budwk.app.util.poiUtils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.budwk.app.sqfw.dto.FireHiddenOfficeWordDTO;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.enums.WeiQingMenPdfType;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;

import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.generateWordDocument;
import static com.budwk.app.util.poiUtils.CommonFormOfficeUtils.wordToPdfAndUpload;

/**
 * @author yangweimin
 * @since 2023/10/9
 *
 * <p>
 * 威清门街道消防安全文档生成工具类
 * </p>
 */
public class WeiQingMenStreetFireHiddenOfficeUtils {

    private static final String UNIT_NAME = "威清门街道直属";

    /**
     * 生成威清门的安全排查pdf
     *
     * @param formSummary 数据DTO
     * @return pdf生成结果
     */
    public static GenerateWordAndPdfUrlVO createFireHiddenPDF(FireHiddenOfficeWordDTO formSummary) {

        List<String> urls = new ArrayList<>();

        // 应急巡查台账（餐饮店面专用）
        if ("餐饮店面".equals(formSummary.getA61())) {
            String url = createEmergencyFoodAndBeverageInspection(formSummary);
            String urlCopy = null;
            if ("是".equals(formSummary.getIsTrafficEmergency())) {
                urlCopy = createEmergencyFoodAndBeverageInspectionCopy(formSummary);
            }
            if (StringUtils.isNotBlank(url)) urls.add(url);
            if (StringUtils.isNotBlank(urlCopy)) urls.add(urlCopy);
        }

        // 应急巡查台账（店面专用）
        if ("店面".equals(formSummary.getA61())) {
            String url = createEmergencyPatrolOfTheStorefront(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 应急巡查台账（临摊、夜市摊专用）
        if ("临摊、夜市摊".equals(formSummary.getA61())) {
            String url = createEmergencyPatrolOfTheNightMarket(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 应急巡查台账（工地（装修）专用）
        if ("工地（装修）".equals(formSummary.getA61())) {
            String url = createEmergencySiteInspectionRenovation(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 应急巡查台账（危房专用）
        if ("危房".equals(formSummary.getA61())) {
            String url = createEmergencyInspectionOfDangerousHouses(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 应急巡查台账（物管专用）
        if (formSummary.getA61().contains("物管")) {
            String url = createEmergencyInspectionOfLedgerUnitManagement(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 应急巡查台账（重点场所专用）
        if (formSummary.getA61().contains("重点场所") && !formSummary.getA61().contains("物管")) {
            String url = createEmergencyInspectionOfKeyPlace(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 应急巡查台账（三合一）
        if ("是".equals(formSummary.getThreeInOne()) && ("店面".equals(formSummary.getA61()) || "餐饮店面".equals(formSummary.getA61()))) {
            String url = createEmergencyThreeAndOne(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 应急巡查台账（快递）
        if ("快递".equals(formSummary.getA6()) && "是".equals(formSummary.getIsTrafficEmergency())) {
            String url = createEmergencyExpressDelivery(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 餐饮经营服务场所消防安全巡查记录
        if ("餐饮店面".equals(formSummary.getA61())) {
            //获取上一次的巡查
            FireHiddenOfficeWordDTO upFormSummary = formSummary.getUpFormSummary();
            if (upFormSummary != null) {
                String a42 = upFormSummary.getA42();
                //上一次A42的限期整改没有值时，不需要生成上次调查的表单
                if (!StrUtil.isBlank(a42) && JSON.parseArray(a42).getJSONArray(0).size() > 0) {
                    String url = createFireSafetyInCateringEstablishments(upFormSummary, null);
                    if (StringUtils.isNotBlank(url)) urls.add(url);
                }
            }
            //获取此次巡查
            String url = createFireSafetyInCateringEstablishments(formSummary, upFormSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        // 瓶装燃气用户安全管理检查表
        if ("瓶装液化气".equals(formSummary.getA17())) {
            String url = createBottledGasUserSafetyManagement(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        //安全整改通知书
        if (formSummary.getA61().contains("重点场所")) {
            String url = createWorkSafetyRectificationNotice(formSummary);
            if (StringUtils.isNotBlank(url)) urls.add(url);
        }

        //现场整改照片
        //byteArrayOutputStreams.add(createChangePicture(formSummary));

        String name = "危房".equals(formSummary.getA61()) ? formSummary.getA2() + "危房" : formSummary.getA1();
        //String fileName = StrFormatter.format("{}安全排查整治表{}", name, DateUtil.format(new Date(), "yyyyMMdd"));
        //zhangp 2023-12-13 文件名称格式统一
        String downloadFileName = DownLoadFileNameEnum.SAFETY_INVESTIGATE_RENOVATE.getName();
        String fileName = StrFormatter.format("{}{}{}", name, downloadFileName, DateUtil.format(new Date(), "yyyyMMdd"));

        String url = CommonFormOfficeUtils.createConvergePdf(urls, fileName);
        GenerateWordAndPdfUrlVO vo = new GenerateWordAndPdfUrlVO();
        vo.setPdfUrl(url);
        return vo;
    }

    /**
     * 应急巡查台账（餐饮店面专用）1
     * 交通应急巡查台账（餐饮劝酒）2
     * 应急巡查台账（店面专用）3
     * 应急巡查台账（临摊、夜市摊专用）4
     * 应急巡查台账（工地（装修）专用）5
     * 应急巡查台账（危房专用）6
     * 应急巡查台账（单位（物管）专用）7
     * 应急巡查台账（重点场所）8
     * 交通应急巡查台账（快递）9
     * 三合一应急巡查台账（三合一）10
     * 餐饮经营服务场所消防安全巡查记录(上次巡查) 11
     * 餐饮经营服务场所消防安全巡查记录（本次巡查）12
     * 瓶装燃气用户安全管理检查表 13
     * 安全生产整改通知书 14
     *
     * 创建单个pdf
     * @return
     */
    public static String createEmergencyPDF(FireHiddenOfficeWordDTO formSummary) {
        int index = formSummary.getIndex();
        if (index == 1) {
            // 应急巡查台账（餐饮店面专用）
            if ("餐饮店面".equals(formSummary.getA61())) {
                String url = createEmergencyFoodAndBeverageInspection(formSummary);
                return url;
            }
        }
        if (index == 2) {
            // 应急巡查台账（餐饮店面专用-劝酒）
            if ("餐饮店面".equals(formSummary.getA61())) {
                String url = createEmergencyFoodAndBeverageInspectionCopy(formSummary);
                return url;
            }
        }
        if (index == 3) {
            // 应急巡查台账（店面专用）
            if ("店面".equals(formSummary.getA61())) {
                String url = createEmergencyPatrolOfTheStorefront(formSummary);
                return url;
            }
        }
        if (index == 4) {
            // 应急巡查台账（临摊、夜市摊专用）
            if ("临摊、夜市摊".equals(formSummary.getA61())) {
                String url = createEmergencyPatrolOfTheNightMarket(formSummary);
                return url;
            }
        }
        if (index == 5) {
            // 应急巡查台账（工地（装修）专用）
            if ("工地（装修）".equals(formSummary.getA61())) {
                String url = createEmergencySiteInspectionRenovation(formSummary);
                return url;
            }
        }
        if (index == 6) {
            // 应急巡查台账（危房专用）
            if ("危房".equals(formSummary.getA61())) {
                String url = createEmergencyInspectionOfDangerousHouses(formSummary);
                return url;
            }
        }
        if (index == 7) {
            // 应急巡查台账（物管专用）
            if (formSummary.getA61().contains("物管")) {
                String url = createEmergencyInspectionOfLedgerUnitManagement(formSummary);
                return url;
            }
        }
        if (index == 8) {
            // 应急巡查台账（重点场所专用）
            if (formSummary.getA61().contains("重点场所") && !formSummary.getA61().contains("物管")) {
                String url = createEmergencyInspectionOfKeyPlace(formSummary);
                return url;
            }
        }
        if (index == 9) {
            // 应急巡查台账（快递）
            if ("快递".equals(formSummary.getA6())) {
                String url = createEmergencyExpressDelivery(formSummary);
                return url;
            }
        }
        if (index == 10) {
            // 应急巡查台账（三合一）
            if ("是".equals(formSummary.getThreeInOne()) && ("店面".equals(formSummary.getA61()) || "餐饮店面".equals(formSummary.getA61()))) {
                String url = createEmergencyThreeAndOne(formSummary);
                return url;
            }
        }
        if (index == 11) {
            // 餐饮经营服务场所消防安全巡查记录(上次巡查)
            if ("餐饮店面".equals(formSummary.getA61())) {
                //获取上一次的巡查
                FireHiddenOfficeWordDTO upFormSummary = new FireHiddenOfficeWordDTO();
                Sqfw_formSummary uf = formSummary.getUpFormSummary();
                if (uf != null) {
                    BeanUtil.copyProperties(uf, upFormSummary);
                    String a42 = upFormSummary.getA42();
                    //上一次A42的限期整改没有值时，不需要生成上次调查的表单
                    if (!StrUtil.isBlank(a42) && JSON.parseArray(a42).getJSONArray(0).size() > 0) {
                        String url = createFireSafetyInCateringEstablishments(upFormSummary, null);
                        return url;
                    }
                }
            }
        }
        if (index == 12) {
            // 餐饮经营服务场所消防安全巡查记录(本次巡查)
            if ("餐饮店面".equals(formSummary.getA61())) {
                //获取上一次的巡查
                FireHiddenOfficeWordDTO upFormSummary = new FireHiddenOfficeWordDTO();
                //获取此次巡查
                String url = createFireSafetyInCateringEstablishments(formSummary, upFormSummary);
                return url;
            }
        }
        if (index == 13) {
            // 瓶装燃气用户安全管理检查表
            if ("瓶装液化气".equals(formSummary.getA17())) {
                String url = createBottledGasUserSafetyManagement(formSummary);
                return url;
            }
        }
        if (index == 14) {
            //安全整改通知书
            if (formSummary.getA61().contains("重点场所")) {
                String url = createWorkSafetyRectificationNotice(formSummary);
                return url;
            }
        }

        return null;
    }

    /**
     * 餐饮经营服务场所消防安全巡查记录
     *
     * @return 输出流
     */
    private static String createFireSafetyInCateringEstablishments(
            FireHiddenOfficeWordDTO formSummary,
            FireHiddenOfficeWordDTO upFormSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        //组装数据
        dataMap.put("yyyy", DateUtil.year(formSummary.getFillDate()));
        dataMap.put("mm", DateUtil.month(formSummary.getFillDate()) + 1);//月份从0开始,所以要加1
        dataMap.put("dd", DateUtil.dayOfMonth(formSummary.getFillDate()));
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a2", formSummary.getA2());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a19", formSummary.getA19());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        //取出a24的1号数组
        String a24 = formSummary.getA24();
        JSONArray a24s = JSON.parseArray(a24);
        dataMap.put("a24", a24s.get(0));
        //现场整改
        String a41 = formSummary.getA41();
        List<String> a41s = JSON.parseArray(JSON.parseArray(a41).getJSONArray(0).toString(), String.class);
        dataMap.put("a41", String.join("、", a41s));
        //获取期限整改
        String a42 = formSummary.getA42();
        List<String> a42s_1 = new ArrayList<>();
        dataMap.put("a41_a42", JSON.parseArray(formSummary.getA41()).getJSONArray(0).size() +
                JSON.parseArray(formSummary.getA42()).getJSONArray(0).size());
        dataMap.put("a42", "");
        dataMap.put("a30_m", "");
        dataMap.put("a30_d", "");
        dataMap.put("mm_1", "");
        dataMap.put("dd_1", "");
        dataMap.put("a42_1", "");
        dataMap.put("a42_2", "");
        dataMap.put("a19_1", "");
        dataMap.put("a20_1", null);
        if (!StrUtil.isBlank(a42)) {
            JSONArray a42s = JSON.parseArray(a42);
            a42s_1 = JSON.parseArray(a42s.get(0).toString(), String.class);
            StringBuffer stringBuffer = new StringBuffer();
            for (Object o : a42s_1) {
                stringBuffer.append(o).append("、");
            }
            if (a42s_1.size() > 0)
                dataMap.put("a42", stringBuffer.substring(0, stringBuffer.lastIndexOf("、")));
        }
        //复查问题
        boolean isHaveProblem = false;
        if (upFormSummary != null) {
            String a42Up = upFormSummary.getA42();
            //获取限期整改问题
            List<String> a42Ups_1 = new ArrayList<>();
            ;
            if (!StrUtil.isBlank(a42Up)) {
                JSONArray a42Ups = JSON.parseArray(a42Up);
                a42Ups_1 = JSON.parseArray(a42Ups.get(0).toString(), String.class);
            }
            if (a42Ups_1.size() != 0) {
                isHaveProblem = true;
                //获取已整改问题
                Collection<String> a42_1 = CollectionUtils.subtract(a42Ups_1, a42s_1);
                StringBuffer s1 = new StringBuffer();
                for (String s : a42_1) {
                    s1.append(s).append("、");
                }
                dataMap.put("a42_1", s1.length() > 0 ? s1.substring(0, s1.lastIndexOf("、")) : "");
                //获取未整改问题
                Collection<String> a42_2 = CollectionUtils.intersection(a42Ups_1, a42s_1);
                StringBuffer s2 = new StringBuffer();
                for (String s : a42_2) {
                    s2.append(s).append("、");
                }
                dataMap.put("a42_2", s2.length() > 0 ? s2.substring(0, s2.lastIndexOf("、")) : "");
                //复查日期
                dataMap.put("mm_1", DateUtil.month(formSummary.getFillDate()) + 1);
                dataMap.put("dd_1", DateUtil.dayOfMonth(formSummary.getFillDate()));
                //复查人
                dataMap.put("a19_1", formSummary.getA19());
                //dataMap.put("a20_1", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
            }
        }
        //期限整改时间
        String a30 = formSummary.getA30();
        if (!StrUtil.isBlank(a30) && a42s_1.size() > 0) {
            Date date = DateUtil.parse(a30, DatePattern.CHINESE_DATE_PATTERN);
            dataMap.put("a30_m", DateUtil.month(date) + 1);
            dataMap.put("a30_d", DateUtil.dayOfMonth(date));
        }

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, null,
                "weiQingMen/fireSafetyInCateringEstablishments.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "被巡查单位人员签名", formSummary.getA20(),
                    140, -10, 65);
            if (upFormSummary != null && isHaveProblem) {
                xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "被复查单位人员签名", formSummary.getA20(),
                        140, -10, 65);
            }
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.FIRE_SAFETY_IN_CATERING_ESTABLISHMENTS.getName(), "").getPdfUrl();
    }


    /**
     * 创建瓶装燃气用户安全管理检查表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createBottledGasUserSafetyManagement(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("area", formSummary.getArea());
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a24", JSON.parseArray(formSummary.getA24()).getJSONArray(1));

        dataMap.put("a19", formSummary.getA19());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, null,
                "weiQingMen/bottledGasUserSafetyManagement.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "受检单位负责人签字", formSummary.getA20(),
                    155, 0, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.BOTTLED_GAS_USER_SAFETY_MANAGEMENT.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（临摊、夜市摊专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyPatrolOfTheNightMarket(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a2", formSummary.getA2());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a10", formSummary.getA10());
        dataMap.put("a11", formSummary.getA11());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        dataMap.put("a38", getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5)));
        //设置图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap,
                "weiQingMen/emergencyPatrolOfTheNightMarket.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        //设置整改照片
        setRectifyAndReformPicture(formSummary, dataMap, footerMap, byteArrayOutputStreams);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（餐饮店面专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyFoodAndBeverageInspection(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a15", formSummary.getA15());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a33", formSummary.getA33());
        dataMap.put("a34", formSummary.getA34());
        dataMap.put("a10", formSummary.getA10());
        dataMap.put("a11", formSummary.getA11());
        dataMap.put("a12", formSummary.getA12());
        dataMap.put("a13", formSummary.getA13());
        dataMap.put("a14", formSummary.getA14());
        dataMap.put("a7", formSummary.getA7());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        String a38 = getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5));
        dataMap.put("a38", a38);
        //设置门头图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap,
                "weiQingMen/emergencyFoodAndBeverageInspection.docx");

        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        //设置整改照片
        setRectifyAndReformPicture(formSummary, dataMap, footerMap, byteArrayOutputStreams);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（餐饮店面专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyFoodAndBeverageInspectionCopy(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a15", formSummary.getA15());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a33", formSummary.getA33());
        dataMap.put("a34", formSummary.getA34());
        dataMap.put("a10", formSummary.getA10());
        dataMap.put("a11", formSummary.getA11());
        dataMap.put("a12", formSummary.getA12());
        dataMap.put("a13", formSummary.getA13());
        dataMap.put("a14", formSummary.getA14());
        dataMap.put("a7", formSummary.getA7());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        //String a38 = getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5));
        //dataMap.put("a38", "无".equals(a38) ? "" : a38);
        //设置图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap,
                "weiQingMen/emergencyFoodAndBeverageInspectionCopy.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.TRAFFIC_EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（店面专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyPatrolOfTheStorefront(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a33", formSummary.getA33());
        dataMap.put("a34", formSummary.getA34());
        dataMap.put("a10", formSummary.getA10());
        dataMap.put("a13", formSummary.getA13());
        dataMap.put("a14", formSummary.getA14());
        dataMap.put("a7", formSummary.getA7());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        String a38 = getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5));
        dataMap.put("a38", a38);
        //设置图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap,
                "weiQingMen/emergencyPatrolOfTheStorefront.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        //设置整改照片
        setRectifyAndReformPicture(formSummary, dataMap, footerMap, byteArrayOutputStreams);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（危房专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyInspectionOfDangerousHouses(FireHiddenOfficeWordDTO formSummary) {
        Map<String, Object> dataMap = Maps.newHashMap();
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a2", formSummary.getA2());
        dataMap.put("a15", formSummary.getA15());
        dataMap.put("phone", formSummary.getCreateUserPhone());
        dataMap.put("a10", formSummary.getA10());
        dataMap.put("a45", formSummary.getA45());
        dataMap.put("a46", formSummary.getA46());
        dataMap.put("a47", formSummary.getA47());
        dataMap.put("a14", formSummary.getA14());
        dataMap.put("a38", getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5)));
        dataMap.put("pictures", getDoorPicture(formSummary));

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap,
                "weiQingMen/emergencyInspectionOfDangerousHouses.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（物管专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyInspectionOfLedgerUnitManagement(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a33", formSummary.getA33());
        dataMap.put("a34", formSummary.getA34());
        dataMap.put("a21", formSummary.getA21());
        dataMap.put("a22", formSummary.getA22());
        dataMap.put("a13", formSummary.getA13());
        dataMap.put("a14", formSummary.getA14());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        dataMap.put("a38", getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5)));
        //设置图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        String templateName = "weiQingMen/emergencyInspectionOfLedgerUnitManagement.docx";

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap, templateName);
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        //设置整改照片
        setRectifyAndReformPicture(formSummary, dataMap, footerMap, byteArrayOutputStreams);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（重点场所）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyInspectionOfKeyPlace(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a33", formSummary.getA33());
        dataMap.put("a34", formSummary.getA34());
        dataMap.put("a21", formSummary.getA21());
        dataMap.put("a22", formSummary.getA22());
        dataMap.put("a13", formSummary.getA13());
        dataMap.put("a14", formSummary.getA14());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        dataMap.put("a38", getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5)));
        //设置图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        String templateName = "weiQingMen/emergencyInspectionOfKeyPlace.docx";

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap, templateName);
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        //设置整改照片
        setRectifyAndReformPicture(formSummary, dataMap, footerMap, byteArrayOutputStreams);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（工地（装修）专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencySiteInspectionRenovation(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a12", formSummary.getA12());
        dataMap.put("a57", formSummary.getA57());
        dataMap.put("a58", formSummary.getA58());
        dataMap.put("a59", formSummary.getA59());
        dataMap.put("a60", formSummary.getA60());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        dataMap.put("a38", getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5)));
        //设置图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap,
                "weiQingMen/emergencySiteInspectionRenovation.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        //设置整改照片
        setRectifyAndReformPicture(formSummary, dataMap, footerMap, byteArrayOutputStreams);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（三合一专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyThreeAndOne(
            FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a15", formSummary.getA15());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a28", formSummary.getA28());
        dataMap.put("a33", formSummary.getA33());
        dataMap.put("a34", formSummary.getA34());
        dataMap.put("a10", formSummary.getA10());
        dataMap.put("a11", formSummary.getA11());
        dataMap.put("a12", formSummary.getA12());
        dataMap.put("a13", formSummary.getA13());
        dataMap.put("a14", formSummary.getA14());
        dataMap.put("a7", formSummary.getA7());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        //String a38 = getA38_A31(formSummary, Arrays.asList(0, 1, 2, 3, 4, 5));
        //dataMap.put("a38", "无".equals(a38) ? "" : a38);
        //设置图片
        setThreeInOnePicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap,
                "weiQingMen/emergencyThreeAndOne.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.THREE_IN_ONE_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 应急巡查台账（快递专用）表
     *
     * @param formSummary 填表数据
     * @return 结果
     */
    private static String createEmergencyExpressDelivery(FireHiddenOfficeWordDTO formSummary) {
        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        dataMap.put("createDate", DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        dataMap.put("a19", formSummary.getA19());
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a3", formSummary.getA3());
        dataMap.put("a4", formSummary.getA4());
        dataMap.put("a33", formSummary.getA33());
        dataMap.put("a34", formSummary.getA34());
        dataMap.put("a10", formSummary.getA10());
        dataMap.put("a13", formSummary.getA13());
        dataMap.put("a14", formSummary.getA14());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.8F));
        dataMap.put("a38", getA38_A31(formSummary, Arrays.asList(-1)));
        //设置图片
        setDoorPicture(formSummary, dataMap);

        Map<String, Object> footerMap = Maps.newHashMap();
        footerMap.put("footer", getFooter(formSummary));

        String templateName = "weiQingMen/emergencyExpressDelivery.docx";

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, footerMap, templateName);
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "※", PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -53, 115);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人", formSummary.getA20(),
                    50, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        //设置整改照片
        setRectifyAndReformPicture(formSummary, dataMap, footerMap, byteArrayOutputStreams);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.TRAFFIC_EMERGENCY_INSPECTION.getName(), "").getPdfUrl();
    }

    /**
     * 安全生产整改通知书
     *
     * @return 输出流
     */
    private static String createWorkSafetyRectificationNotice(FireHiddenOfficeWordDTO formSummary) {
        String a38 = getA38(formSummary, Arrays.asList(0, 1, 2));
        if ("无".equals(a38)) return null;

        List<ByteArrayOutputStream> byteArrayOutputStreams = new ArrayList<>();
        Map<String, Object> dataMap = Maps.newHashMap();
        //组装数据
        dataMap.put("yyyy", DateUtil.year(formSummary.getFillDate()));
        dataMap.put("mm", DateUtil.month(formSummary.getFillDate()) + 1);//月份从0开始,所以要加1
        dataMap.put("dd", DateUtil.dayOfMonth(formSummary.getFillDate()));
        dataMap.put("a1", formSummary.getA1());
        dataMap.put("a38", a38);

        if (StringUtils.isNotBlank(formSummary.getA30())) {
            Date date = DateUtil.parse(formSummary.getA30(), DatePattern.CHINESE_DATE_PATTERN);
            dataMap.put("a30_y", DateUtil.year(date));
            dataMap.put("a30_m", DateUtil.month(date) + 1);
            dataMap.put("a30_d", DateUtil.dayOfMonth(date));
        } else {
            dataMap.put("a30_y", null);
            dataMap.put("a30_m", null);
            dataMap.put("a30_d", null);
        }

        dataMap.put("a19", formSummary.getA19());
        //dataMap.put("a20", StrUtil.isBlank(formSummary.getA20()) ? null : addWordPicture(formSummary.getA20(), 1.0F));

        XWPFTemplate xwpfTemplate = generateWordDocument(dataMap, null,
                "weiQingMen/workSafetyRectificationNotice.docx");
        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "盖章",
                    PictureUtils.getLocalImagePath(formSummary.getUnitName()),
                    280, -8, 125);
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "签收人签名", formSummary.getA20(),
                    130, -5, 65);
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            xwpfDocument.write(wordOutPutStream);
            byteArrayOutputStreams.add(wordOutPutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PictureUtils.close(xwpfTemplate, xwpfDocument);

        return wordToPdfAndUpload(byteArrayOutputStreams,
                WeiQingMenPdfType.WORK_SAFETY_RECTIFICATION_NOTICE.getName(), "").getPdfUrl();
    }

    /**
     * 现场整改照片
     *
     * @param formSummary
     * @return
     */
    private static ByteArrayOutputStream createChangePicture(FireHiddenOfficeWordDTO formSummary) {
        if (StringUtils.isNotBlank(formSummary.getA43())) {
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(formSummary.getA43().split(",")));
            String textStr = "附件：{} {} 现场整改照片";
            textStr = StrFormatter.format(textStr, formSummary.getA1(), DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
            return PictureUtils.getByteArrayOutputStream(textStr, pictureList, "weiQingMen/pictures.docx");
        }
        return null;
    }

    private static String getA38(FireHiddenOfficeWordDTO formSummary, List<Integer> nums) {
        StringBuffer stringBuffer = new StringBuffer();
        JSONArray jsonArray = JSON.parseArray(formSummary.getA38());
        if (jsonArray == null) return null;
        JSONArray a38 = new JSONArray();
        if (nums.contains(0)) a38.addAll(jsonArray.getJSONArray(0));
        if (nums.contains(1)) a38.addAll(jsonArray.getJSONArray(1));
        if (nums.contains(2)) a38.addAll(jsonArray.getJSONArray(2));
        if (jsonArray.size() > 3) {
            if (nums.contains(3)) a38.addAll(jsonArray.getJSONArray(3));
            if (nums.contains(4)) a38.addAll(jsonArray.getJSONArray(4));
            if (nums.contains(5)) a38.addAll(jsonArray.getJSONArray(5));
        }
        int i = 0;
        for (Object o : a38) {
            if (!"无".equals(o.toString())) {
                i++;
                stringBuffer.append(i + "、");
                String s = o.toString();
                //去掉序号
                stringBuffer.append(s.substring(s.indexOf(".") + 1));
            }
        }
        return stringBuffer.length() == 0 ? "无" : stringBuffer.toString();
    }

    private static String getA38_A31(Sqfw_formSummary formSummary, List<Integer> nums) {
        StringBuffer stringBuffer = new StringBuffer();
        JSONArray jsonArray = JSON.parseArray(formSummary.getA38());
        if (jsonArray == null) return null;
        JSONArray a38 = new JSONArray();
        if (nums.contains(0)) a38.addAll(JSON.parseArray(formSummary.getA38()).getJSONArray(0));
        if (nums.contains(1)) a38.addAll(JSON.parseArray(formSummary.getA38()).getJSONArray(1));
        if (nums.contains(2)) a38.addAll(JSON.parseArray(formSummary.getA38()).getJSONArray(2));
        if (jsonArray.size() > 3) {
            if (nums.contains(3)) a38.addAll(JSON.parseArray(formSummary.getA38()).getJSONArray(3));
            if (nums.contains(4)) a38.addAll(JSON.parseArray(formSummary.getA38()).getJSONArray(4));
            if (nums.contains(5)) a38.addAll(JSON.parseArray(formSummary.getA38()).getJSONArray(5));
        }
        if (!StrUtil.isBlank(formSummary.getA31())) a38.add(formSummary.getA31());
        int i = 0;
        for (Object o : a38) {
            if (!"无".equals(o.toString())) {
                i++;
                stringBuffer.append(i + "、");
                String s = o.toString();
                //去掉序号
                stringBuffer.append(s.substring(s.indexOf(".") + 1));
            }
        }
        return stringBuffer.length() == 0 ? "无" : stringBuffer.toString();
    }

    /**
     * 设置图片（三合一专用）
     *
     * @param formSummary
     * @return
     */
    private static boolean setThreeInOnePicture(Sqfw_formSummary formSummary, Map<String, Object> dataMap) {
        //门头照
        String door_title = "附件：{} {} 门头照、三合一照片";
        door_title = StrFormatter.format(door_title, formSummary.getA1(),
                DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        List<Map> door_pictures = new ArrayList<>();
        String a18 = formSummary.getA18();
        if (!StrUtil.isBlank(a18)) {
            String[] door_picture = a18.split(",");
            for (String url : door_picture) {
                if (!StrUtil.isBlank(url)) {
                    Map<String, Object> pictureMap = Maps.newHashMap();
                    pictureMap.put("picture", PictureUtils.scaleImage(url, 180, false));
                    door_pictures.add(pictureMap);
                    break;
                }
            }
        }
        String threeInOneImg = formSummary.getThreeInOneImg();
        if (!StrUtil.isBlank(threeInOneImg)) {
            String[] door_picture = threeInOneImg.split(",");
            int i = 0;
            for (String url : door_picture) {
                if (!StrUtil.isBlank(url) && i < 2) {
                    i ++;
                    Map<String, Object> pictureMap = Maps.newHashMap();
                    pictureMap.put("picture", PictureUtils.scaleImage(url, 180, false));
                    door_pictures.add(pictureMap);
                }
            }
        }

        dataMap.put("door_title", door_pictures.size() == 0 ? "" : door_title);
        dataMap.put("door_pictures", door_pictures);
        return door_pictures.size() == 0 ? false : true;
    }

    /**
     * 设置门头照(除危房外)
     *
     * @param formSummary
     * @return
     */
    private static boolean setDoorPicture(Sqfw_formSummary formSummary, Map<String, Object> dataMap) {
        //门头照
        String door_title = "附件：{} {} 门头照";
        door_title = StrFormatter.format(door_title, formSummary.getA1(),
                DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        List<Map> door_pictures = new ArrayList<>();
        String a18 = formSummary.getA18();
        if (!StrUtil.isBlank(a18)) {
            String[] door_picture = a18.split(",");
            for (String url : door_picture) {
                if (!StrUtil.isBlank(url)) {
                    Map<String, Object> pictureMap = Maps.newHashMap();
                    pictureMap.put("picture", PictureUtils.scaleImage(url, 240, false));
                    door_pictures.add(pictureMap);
                }
            }
        }
        dataMap.put("door_title", door_pictures.size() == 0 ? "" : door_title);
        dataMap.put("door_pictures", door_pictures);
        return door_pictures.size() == 0 ? false : true;
    }

    /**
     * 设置整改照片(除危房外)
     *
     * @param formSummary
     * @return
     */
    private static boolean setChangePicture(Sqfw_formSummary formSummary, Map<String, Object> dataMap) {
        if (StringUtils.isBlank(formSummary.getA43())) {
            dataMap.put("before_title", "");
            dataMap.put("before_pictures", new ArrayList<>());
            dataMap.put("after_title", "");
            dataMap.put("after_pictures", new ArrayList<>());
            return false;
        }
        //因为老数据的a43不是数组，所以要在此判断
        boolean isArray = true;
        JSONArray a43 = null;
        try {
            a43 = JSON.parseArray(formSummary.getA43());
        } catch (Exception e) {
            isArray = false;
        }
        //整改前
        String before_title = "附件：{} {} 整改前照片";
        before_title = StrFormatter.format(before_title, formSummary.getA1(),
                DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd"));
        List<Map> before_pictures = new ArrayList<>();
        String[] before_picture = isArray ? a43.getString(0).split(",") : formSummary.getA43().split(",");
        for (String url : before_picture) {
            if (!StrUtil.isBlank(url)) {
                Map<String, Object> pictureMap = Maps.newHashMap();
                pictureMap.put("picture", PictureUtils.scaleImage(url, 240, false));
                before_pictures.add(pictureMap);
            }
        }
        dataMap.put("before_title", before_pictures.size() == 0 ? "" : before_title);
        dataMap.put("before_pictures", before_pictures);
        //整改后
        String after_title = "附件：{} {} 整改后照片";
        String after_date = DateUtil.format(formSummary.getFillDate(), "yyyy-MM-dd");
        // a63为整改后图片更新的时间，如果不为空，则取这个时间(该字段格式：yyyy-MM-dd HH:mm:ss)
        String a63 = formSummary.getA63();
        if (!StrUtil.isBlank(a63)) {
            after_date = a63.substring(0, 10);
        }
        after_title = StrFormatter.format(after_title, formSummary.getA1(), after_date);
        List<Map> after_pictures = new ArrayList<>();
        String[] after_picture = isArray ? a43.getString(1).split(",") : formSummary.getA43().split(",");
        for (String url : after_picture) {
            if (!StrUtil.isBlank(url)) {
                Map<String, Object> pictureMap = Maps.newHashMap();
                pictureMap.put("picture", PictureUtils.scaleImage(url, 240, false));
                after_pictures.add(pictureMap);
            }
        }
        dataMap.put("after_title", after_pictures.size() == 0 ? "" : after_title);
        dataMap.put("after_pictures", after_pictures);
        return before_pictures.size() + after_pictures.size() == 0 ? false : true;
    }

    /**
     * 获取门头照(危房专用)
     *
     * @param formSummary
     * @return
     */
    private static List<Map> getDoorPicture(Sqfw_formSummary formSummary) {
        if (StringUtils.isBlank(formSummary.getA43())) {
            return new ArrayList<>();
        }
        //因为老数据的a43不是数组，所以要在此判断
        boolean isArray = true;
        JSONArray a43 = null;
        try {
            a43 = JSON.parseArray(formSummary.getA43());
        } catch (Exception e) {
            isArray = false;
        }
        List<Map> pictures = new ArrayList<>();
        String[] morning = isArray ? a43.getString(2).split(",") : formSummary.getA43().split(",");
        String[] noon = isArray ? a43.getString(3).split(",") : formSummary.getA43().split(",");
        String[] night = isArray ? a43.getString(4).split(",") : formSummary.getA43().split(",");
        for (String url : morning) {
            Map<String, Object> pictureMap = Maps.newHashMap();
            pictureMap.put("picture", PictureUtils.scaleImage(url, 180, true));
            pictures.add(pictureMap);
        }
        for (String url : noon) {
            Map<String, Object> pictureMap = Maps.newHashMap();
            pictureMap.put("picture", PictureUtils.scaleImage(url, 180, true));
            pictures.add(pictureMap);
        }
        for (String url : night) {
            Map<String, Object> pictureMap = Maps.newHashMap();
            pictureMap.put("picture", PictureUtils.scaleImage(url, 180, true));
            pictures.add(pictureMap);
        }
        return pictures;
    }

    private static String getFooter(Sqfw_formSummary formSummary) {
        String str = "居委会";
        return UNIT_NAME.equals(formSummary.getUnitName()) ? "威清门应急服务中心" :
                formSummary.getCommunity() + str;
    }

    /**
     * 设置整改照片
     * @param formSummary
     * @param dataMap
     * @param footerMap
     * @param byteArrayOutputStreams
     */
    private static void setRectifyAndReformPicture(Sqfw_formSummary formSummary, Map<String, Object> dataMap, Map<String,
            Object> footerMap, List<ByteArrayOutputStream> byteArrayOutputStreams) {
        //设置整改照片
        boolean isHave = setChangePicture(formSummary, dataMap);
        if (isHave) {
            XWPFTemplate xwpfTemplate1 = generateWordDocument(dataMap, footerMap,
                    "weiQingMen/rectifyAndReformPicture.docx");
            ByteArrayOutputStream wordOutPutStream = new ByteArrayOutputStream();
            try {
                xwpfTemplate1.write(wordOutPutStream);
                byteArrayOutputStreams.add(wordOutPutStream);
                xwpfTemplate1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
